﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ALDIOrganizer
{
    public partial class WorkingHours : Form
    {        
        public WorkingHoursSpan _workingHours;
     
        private bool valid;
                
        public WorkingHours()
        {
            InitializeComponent();
        }

        public WorkingHours(WorkingHoursSpan[] inputWhrs, int day, bool monfri) : this()
        {
            if (!monfri)
            {
                this.Height -= pnlApply.Height;
                pnlApply.Visible = false;
            }
            // invalid input parameters
            if (inputWhrs.Length != 7) throw new ApplicationException("formWorkingHours constructor: workingHRS[] _inputWHRS contains "+inputWhrs.Length+" elements instead of 7");
            if ((day < 0) || (day > 6)) throw new ApplicationException("formWorkingHours constructor: int day = " + day.ToString() + ", it should be within 0...6 range");

            // storing Initial parameters     
            _workingHours = inputWhrs[day];

            tbFrom.Text = _workingHours.Open.ToString("d2");
            tbFrom.Select(0, 0);
            tbTo.Text = _workingHours.Close.ToString("d2");
            
            
            

            valid = true;

            Text = Time.NameOfDay(day);
        }

        private void UpdateData()
        {
            string _input = String.Concat(tbFrom.Text, "-", tbTo.Text);
            Shift _s = new Shift();
            
            valid = true; // optimistic assumption
                      

            if(!(Shift.IsShift(_input)))
                valid = false;
            else
                try
                {
                    _s = Shift.ToShift(_input, null, false);
                }
                catch
                {
                    valid = false;
                }
            
            if (valid) // that'pShift how we know an pException wasn't thrown
            {
                _workingHours.Open = _s.Start;
                _workingHours.Close = _s.Finish;
            }
            btnOK.Enabled = valid;
        }        

        private void tbFrom_TextChanged(object sender, EventArgs e)
        {
            UpdateData();
        }

        private void tbTo_TextChanged(object sender, EventArgs e)
        {
            UpdateData();
        }

        
        

        
    }
}
